package com.googlecode.horncomb.web.spring.mvc.support;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;

import com.googlecode.horncomb.web.spring.mvc.BackingObjectFactory;

public class SimpleBackingObjectFactory implements BackingObjectFactory {
	protected final Log logger = LogFactory.getLog(getClass());

	public Object createBackingObject(HttpServletRequest request,
			Class<?> commandClass) throws Exception {
		if (commandClass == null) {
			throw new IllegalStateException(
					"Cannot create command without commandClass being set - "
							+ "either set commandClass or (in a form controller) override formBackingObject");
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Creating new command of class ["
					+ commandClass.getName() + "]");
		}
		return BeanUtils.instantiateClass(commandClass);
	}

}
